<?php

namespace App\Services;

use App\Models\AqModel;
use Slowlyo\OwlAdmin\Services\AdminService;

/**
 * 问答模型
 *
 * @method AqModel getModel()
 * @method AqModel|\Illuminate\Database\Query\Builder query()
 */
class AqModelService extends AdminService
{
	protected string $modelName = AqModel::class;

	public function searchable($query)
	{
		parent::searchable($query);

		$query->when($this->request->input('enabled'), fn($q) => $q->where('enabled', $this->request->input('enabled')));
	}

    public function sortable($query)
    {
        if (request()->orderBy && request()->orderDir) {
            $query->orderBy(request()->orderBy, request()->orderDir ?? 'asc');
        } else {
            $query->orderBy('displayorder');
        }
    }

    public function saving(&$data, $primaryKey = '')
    {
        if(!is_array($data['group_ids'])){
            $data['group_ids'] = array_filter(explode(',',$data['group_ids']));
        }
    }
}
